'use strict';

const { log } = require("console");

const db = uniCloud.database()
const dbCmd = db.command;

exports.main = async (event, context) => {
	const {
		title,cover,author
	} = event;

	// let userInfo = await db.collection('user').doc(userId).get()
	// let article_ids = userInfo.data[0].article_ids; 
	function generateCustomAlphaNumeric() {
	    const numbers = Math.floor(Math.random() * 1000);
	    const letters = String.fromCharCode(65 + Math.floor(Math.random() * 26));
	    return numbers + letters;
	}
	let id=generateCustomAlphaNumeric()
	let res=await db.collection('article').add({title:title,cover:cover,author,id,mode:"column"})
	let arr=[...author.article_ids,id]
	// console.log(111,res)
let res2=await db.collection('user').where({
    '_id': author._id
	}).update({
    'article_ids': arr
	})
	// const list = await db.collection('article')
	// 	.aggregate()
	// 	.project({
	// 		content: 0,
	// 		comments:0
	// 	})
	// 	.match({
	// 		id:dbCmd.in(article_ids)
	// 	})
	// 	.end();

	//返回数据给客户端
	return {
		code: 0,
		msg: "请求成功",
		data: res
	}
};
